

<#include "/base/common/head.html"/>
    
    <table id="dg"></table>
    <div id="tb" style="padding:2px 5px;">
        <div class="wrap_search">
		     <input type="hidden" name="roleId" value="${roleId!}" />
        </div>
        <div style="text-align: left; margin:6px;">
        	<!--  
        	
            -->
            <span> &nbsp;权限设置&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>
	        <a href="javascript:void(0)" class="easyui-linkbutton addBtn" iconCls="glyphicon-floppy-disk" plain="true" onclick="saveAuth()" style="color: #398DEE" >保存</a>
        </div>
    </div>
    
<style type="text/css">

.l-btn-icon, .menu-icon{ color: #398DEE; }

</style>   
    
<script type="text/javascript">

//数据表格初始化参数
var dgOptions = {
		idField:"id",
		treeField:"name",
        rownumbers:true, //显示行号
    	fit:true, //自适应父容器
    	border:false, //是否显示面板边框。
    	url:'findAllResource',  //数据请求URL
    	method:'post', //请求方式
    	toolbar:'#tb', //工具栏
    	//pageSize: 20, //每页数量
    	//pagination:true, //显示分页
    	//fitColumns:true, //自动调整各列宽度。当为true时，各列的宽度值只是一个比例。
    	striped:true, //是否显示斑马线
    	multiSort:false, //是否允许多列排序
		sortName:'orderNum', //初始排序字段
		sortOrder:'asc', //初始排序方式
    	singleSelect: false, //是否单选
		ctrlSelect : true, //启用多选时，允许使用Ctrl键+鼠标进行多选
		checkOnSelect:false,
		selectOnCheck : false,
		showFooter : true, //是否显示行脚
		nowrap:false,
        queryParams: zcurdGetParam(), //请求参数
        columns: [[
		         {field:'id', checkbox:true}
	           	 	,{field:'name', title: '菜单', width:180, align:'left'   }
	           	 	,{field:'son', title: '按钮', width:400, align:'left' ,formatter:function(value,row,index){return formatButton(value,row,index);}}
	           	 
	           ]],
	    loadFilter: function(data) {
	    	  		return data;
	           }
	   
	};

var datagrid = $("#dg"); //数据表格

$(function() {
	datagrid.treegrid(dgOptions); //绘制数据表格
	
	datagrid.treegrid({
		onCheck:function(row)
		{//选中一个菜单复选框时
			//同步选中下面的所有按钮
			$("input[fatherId='"+row.id+"']").prop("checked", true); //按钮
			
			//选取所有子元素
			var children = datagrid.treegrid('getChildren',row.id);
			for(var i=0; i<children.length; i++)
			{
				var one = children[i];
				$("input[name='id'][value='"+one.id+"']").prop("checked", true); //子菜单
				$("input[fatherId='"+one.id+"']").prop("checked", true); //子菜单按钮
			} 
			
		},
		onUncheck:function(row)
		{//取消一个菜单复选框时
			//同步取消下面的所有按钮
			$("input[fatherId='"+row.id+"']").prop("checked", false) //按钮
			
			//取消所有子元素
			var children = datagrid.treegrid('getChildren',row.id);
			for(var i=0; i<children.length; i++)
			{
				var one = children[i];
				$("input[name='id'][value='"+one.id+"']").prop("checked", false); //子菜单
				$("input[fatherId='"+one.id+"']").prop("checked", false); //子菜单按钮
			} 
			
		},
		onCheckAll:function(rows)
		{
			$("input[fatherId]").prop("checked", true)
		},
		onUncheckAll:function(rows)
		{
			$("input[fatherId]").prop("checked", false)
		}
	});

});

//获取查询参数
function zcurdGetParam() {
	var param = {};
	$("#tb :input[name]").each(function(i, item) {
		if($(item).val()) {
			param[$(item).attr("name")] = $(item).val();	
		}
	});
	return param;
}



//保存
function saveAuth() {
	var ids = [];
	
	$.each($("input[name='id']:checked"), function(i, item) {
		ids.push($(item).val());
	});
	
	$.post(getCurrUrl("editAuth"), {ids: ids.join(","),roleId:${roleId!}}, function(data) {
		
		if("01" == data.code){//成功
			showMsg(data.msg); //显示消息
		}else if("04" == data.code){//失败
			showErrorMsg(data.msg); //显示错误消息
		}
		
	});
}
	
//格式化 铵钮显示
function formatButton(value,row,index)
{	
	if(value == null)
	{
		return "";
	}
	var str="";
	for(var i=0; i < value.length;i++)
	{
		var one = value[i];
		var ch="";
		if(one.str1=="1")
		{
			ch="checked";
		}
		//此处的展示复选框，是行左侧的复选框一样的name。本身都是“资源”，提交时可以进行相同处理。
		str =str + '<input name="id" fatherId="'+row.id+'" value="' + one.id + '" type="checkbox" '+ch+'/>' + one.name+'&nbsp;&nbsp;';
	}
	
	return str;
}	

</script>

<#include "/base/common/dialogWindow.html"/>
<#include "/base/common/foot.html"/>
